package com.czz.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.common.exception.CustomerException;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.portal.entity.Course;
import com.czz.portal.entity.Order;
import com.czz.portal.entity.Period;
import com.czz.portal.mapper.CourseMapper;
import com.czz.portal.mapper.OrderMapper;
import com.czz.portal.mapper.PeriodMapper;
import com.czz.portal.query.OrderQuery;
import com.czz.portal.service.OrderService;
import com.czz.portal.vo.BillVo;
import com.czz.portal.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: OrderServiceImpl
 * @Author Czz
 * @Date 2020/2/9
 * @Time 16:30
 **/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    PeriodMapper periodMapper;

    @Override
    public OrderVo getOrderVoById(String id) {
        OrderVo orderVo = new OrderVo();

        //查询订单
        Order order = orderMapper.selectById(id);

        if (ObjectUtils.isEmpty(order)){
            throw new CustomerException(ResultCodeEnum.NOT_FOUND);
        }

        //查询课程
        String courseId = order.getCourseId();
        Course course = courseMapper.selectById(courseId);

        BeanUtils.copyProperties(order,orderVo);
        orderVo.setCover(course.getCover());

        return orderVo;
    }

    @Override
    public List<OrderVo> getOrderPageList(Page<OrderVo> pageParam, OrderQuery orderQuery) {
        return orderMapper.getOrderVoPageList(pageParam,orderQuery);
    }

    @Override
    public void cancelOrderById(String id) {
        Order order = orderMapper.selectById(id);
        // 判断订单状态，只有未支付订单才能取消
        if (!order.getStatus().equals(0)){
            return;
        }
        order.setStatus(2);
        orderMapper.updateById(order);
    }

    @Override
    public List<BillVo> getBillVoPageList(Page<BillVo> pageParam, String userId) {
        return orderMapper.getBillVoPageList(pageParam, userId);
    }

    @Override
    public BigDecimal getUserTotalPaid(String userId) {
        return orderMapper.getUserTotalPaid(userId);
    }

    @Override
    public Integer isUserOrder(String userId,String periodId,String courseId) {
        //课程id为空，则根据课时id查询
        String tempCourseId = null;

        if (StringUtils.isEmpty(courseId)){
            Period period = periodMapper.selectById(periodId);
            tempCourseId = period.getCourseId();
        }
        else
        tempCourseId  = courseId;

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("course_id",tempCourseId);
        queryWrapper.eq("order_status",1);
        Integer count = orderMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public Integer isUserVideo(String userId, String videoId) {
        QueryWrapper<Period> periodQueryWrapper = new QueryWrapper<>();
        periodQueryWrapper.eq("video_source_id",videoId);
        Period period = periodMapper.selectOne(periodQueryWrapper);

        //如果不存在该视频
        if (ObjectUtils.isEmpty(period)){
            throw new CustomerException(ResultCodeEnum.NOT_FOUND);
        }

        //如果为免费课程
        if (period.getFree() == 1) return 1;

        //收费课程
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("course_id",period.getCourseId());
        queryWrapper.eq("order_status",1);
        Integer count = orderMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    @Transactional
    public void saveFreeOrder(Order order) {
        //免费订单，订单状态置为已支付
        order.setStatus(1);
        order.setPayTime(new Date());

        //课程购买数加一
        Course course = courseMapper.selectById(order.getCourseId());
        course.setBuySum(course.getBuySum()+1);
        courseMapper.updateById(course);

        orderMapper.insert(order);
    }

    @Override
    public void saveOrder(Order order) {
        //判断用户是否存在该课程订单
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",order.getUserId());
        queryWrapper.eq("course_id",order.getCourseId());
        Integer count = orderMapper.selectCount(queryWrapper);

        //存在订单
        if (count > 0){
            throw new CustomerException(ResultCodeEnum.RESOURCE_EXISTS);
        }

        //不存在订单
        orderMapper.insert(order);
    }
}
